using System;
using EnvDTE;

namespace Core
{
	public class EditPointHelpers
	{
		public static EditPoint FindNextCamelCaseWord(EditPoint startPoint)
		{
			EditPoint searchPoint = startPoint.CreateEditPoint();
			searchPoint.CharRight();

			EditPoint endPoint = startPoint.CreateEditPoint();
			endPoint.WordRight();

			while (searchPoint.LessThan(endPoint) && IsStartOfWord(GetSearchText(searchPoint)) == false)
				searchPoint.CharRight();

			return searchPoint;
		}

		public static EditPoint FindPreviousCamelCaseWord(EditPoint startPoint)
		{
			EditPoint searchPoint = startPoint.CreateEditPoint();
			searchPoint.CharLeft();

			EditPoint endPoint = startPoint.CreateEditPoint();
			endPoint.WordLeft();

			while (searchPoint.GreaterThan(endPoint) && IsStartOfWord(GetSearchText(searchPoint)) == false)
				searchPoint.CharLeft();

			return searchPoint;
		}

		public static string GetSearchText(EditPoint searchPoint)
		{
			EditPoint left = searchPoint.CreateEditPoint();
			left.CharLeft();

			EditPoint right = searchPoint.CreateEditPoint();
			right.CharRight();

			return left.GetText(right);
		}

		public static bool IsStartOfWord(string s)
		{
			bool symbolThenAlphaNumeric = IsAlphaNumeric(s[0]) == false && IsAlphaNumeric(s[1]);
			bool lowerCaseThenUpperCase = Char.IsLower(s[0]) && Char.IsUpper(s[1]);

			return symbolThenAlphaNumeric || lowerCaseThenUpperCase;
		}

		private static bool IsAlphaNumeric(char c)
		{
			return Char.IsUpper(c) || Char.IsLower(c) || Char.IsNumber(c);
		}
	}
}